import Vue from 'vue'
import VueRouter from 'vue-router'
import routes from './routes'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({showSpinner:false})
Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
  document.title = to.meta && to.meta.title || '饿了么';

  //获取token
  var token = localStorage.getItem('token');
  
  if( to.path == '/index/ding' || to.path == '/index/wude' ){
    if( token ){
      next();
    }else{ 
      next('/index');
    }
  }else{
    next();
  }

})

//注册 全局后置守卫函数( 路由表中每一个路由在跳转之后都会执行 后置守卫函数 )
router.afterEach((to,from)=>{
  NProgress.done(); //结束播放进度条动画
})
export default router
